<html>
<head>
    <script src="../tma.js"></script>
    <script>
        tma.onload = function () {
            // All libraries are loaded asynchronously.
            tma.log("test.html: libraries must be loaded");
            var screen = new TmaScreen(640, 480);
            screen.attachTo(TmaScreen.BODY);
            screen.fill("rgba(0, 0, 0, 1)");
            screen.afterimage("rgba(0, 0, 0, 0.2)");
            screen.blur(1.0, 0.2, 5.0, 0, 0, true);

            function Snow () { TmaParticle.apply(this, arguments); }
            Snow.prototype = new TmaParticle(null, 0);
            Snow.prototype.constructor = Snow;
            Snow.GRAVITY = 10 / 1000;

            var container = new TmaParticle.Container(Snow);

            Snow.emit = function () {
                var x = Math.random() * screen.width;
                var vy = Math.random() + 0.5;
                var s = Math.random();
                var v = Math.random();
                container.add(x, vy, s, v);
            };

            Snow.prototype.initialize = function (x, vy, s, v) {
                // x = screen.width / 2 + R cos(w)
                // y = R (1 - sin(w))
                // R = screen.height * 2
                // w = acos((x - screen.width / 2) / R)
                var r = screen.height * 1;
                var w = Math.acos((x - screen.width / 2) / r);
                this.y = r * (1 - Math.sin(w)) + Math.random() * 16;
                this.x = x;
                this.vy = vy;
                this.h = 0;
                this.s = s;
                this.v = v;
            };

            Snow.prototype.update = function () {
                this.vy += Snow.GRAVITY;
                this.vy *= 0.99;
                this.y += this.vy;
                if (this.y >= screen.height)
                    return false;
                this.h = (this.h + 2) % 360;
                screen.setPixel(~~this.x, ~~this.y, this.h, this.s, this.v,
                        0xff, true);
                return true;
            };

            var count = 0;

            function main () {
                count++;
                if (count > 100) {
                    count = 0;
                    console.log("particles: " + container.length);
                }
                for (i = 0; i < 100; i++)
                    Snow.emit();
                var data = screen.lock(TmaScreen.LOCK_WITH_SCREEN).data;
                container.update();
                screen.unlock();
            }

            setInterval(main, 20);
        };
    </script>
</head>
</html>
